﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;
using MINERP.DAL;
using MINERP.MODEL.Frontier;

namespace MINERP.BLL.Frontier
{
    public class PaperBoxBLL
    {

        /// <summary>
        /// 获得打印机所有纸箱
        /// </summary>
        /// <param name="printerId"></param>
        /// <returns></returns>
        public List<PaperBoxInfo> GetPaperBoxList(int printerId)
        {
            List<PaperBoxInfo> list = new List<PaperBoxInfo>();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<PaperBox> pblist = enty.PaperBox.Where(p=>p.printer.PrinterID ==printerId).ToList();
                foreach (var item in pblist)
                {
                    PaperBoxInfo pb = new PaperBoxInfo();
                    pb.PaperBoxId = item.PaperBoxId;
                    pb.PaperBoxName = item.PaperBoxName;
                    pb.PaperTypeName = item.PaperTypeName;
                    //pb.PaperFaceName = item.PaperFaceName;
                    pb.PaperWidth = item.PaperWidth;
                    pb.PrinterId = printerId;

                    list.Add(pb);

                    
                }
            }
            return list;
        }

        /// <summary>
        /// 设置纸箱当前使用的纸
        /// </summary>
        /// <param name="paperBoxName"></param>
        /// <param name="printerId"></param>
        /// <param name="paperTypeName"></param>
        /// <param name="paperWidth"></param>
        /// <returns></returns>
        public int UpdatePaperBox(string paperBoxName, int printerId,string paperTypeName,double paperWidth )
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
                {

                    PaperBox pb = enty.PaperBox.FirstOrDefault(p => p.printer.PrinterID == printerId && p.PaperBoxName == paperBoxName);
                    if (pb != null)
                    {
                        pb.PaperTypeName = paperTypeName;
                        //pb.PaperFaceName = paperFaceName;
                        pb.PaperWidth = paperWidth;
                        enty.SaveChanges();
                    }
                    else
                    {
                        PaperBox pbox = new PaperBox();
                        pbox.PaperBoxName = paperBoxName;
                        pbox.PaperTypeName = paperTypeName;
                        //pbox.PaperFaceName = paperFaceName;
                        pbox.PaperWidth = paperWidth;
                        pbox.printer = enty.printer.FirstOrDefault(p => p.PrinterID == printerId);
                        enty.AddToPaperBox(pbox);
                        enty.SaveChanges();
                    }


                }
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }
    }
}
